
<div class="pagination pagination-right">
    <ul>
    {% if total > 0 %}
      {% if page == 1 %}
      <li class="active"><a>«</a></li>
      {% elif pages > 1 and page > 1%}
      <li class=""><a href="{{url}}{{prepage}}">«</a></li>
      {% end %}

      {% if pages > 12 %}
        {% if page < 5 %}
          <li {%if 1 == page%}class="active"{%end%}><a href="{{url}}1">1</a></li>
          <li {%if 2 == page%}class="active"{%end%}><a href="{{url}}2">2</a></li>
          <li {%if 3 == page%}class="active"{%end%}><a href="{{url}}3">3</a></li>
          <li {%if 4 == page%}class="active"{%end%}><a href="{{url}}4">4</a></li>
          <li {%if 5 == page%}class="active"{%end%}><a href="{{url}}5">5</a></li>
          <li {%if 6 == page%}class="active"{%end%}><a href="{{url}}6">6</a></li>
          <li><a>...</a></li>
          <li {%if pages-1 == page%}class="active"{%end%}><a href="{{url}}{{pages-1}}">{{pages-1}}</a></li>
          <li {%if pages == page%}class="active"{%end%}><a href="{{url}}{{pages}}">{{pages}}</a></li>
        {% elif endnum > 6 and page > 5%}
          <li><a href="{{url}}1">1</a></li>
          <li><a href="{{url}}2">2</a></li>
          <li><a>...</a></li>
          <li><a href="{{url}}{{page-1}}">{{page-1}}</a></li>
          <li class="active"><a href="{{url}}{{page}}">{{page}}</a></li>
          <li ><a href="{{url}}{{page+1}}">{{page+1}}</a></li>
          <li><a>...</a></li>
          <li><a href="{{url}}{{pages-1}}">{{pages-1}}</a></li>
          <li><a href="{{url}}{{pages}}">{{pages}}</a></li>
        {% elif endnum > 6 and page <= 5 %}
          <li {%if 1 == page%}class="active"{%end%}><a href="#">1</a></li>
          <li {%if 2 == page%}class="active"{%end%}><a href="{{url}}2">2</a></li>
          <li {%if 3 == page%}class="active"{%end%}><a href="{{url}}3">3</a></li>
          <li {%if 4 == page%}class="active"{%end%}><a href="{{url}}4">4</a></li>
          <li {%if 5 == page%}class="active"{%end%}><a href="{{url}}5">5</a></li>
          <li {%if 6 == page%}class="active"{%end%}><a href="{{url}}6">6</a></li>
          <li><a>...</a></li>
          <li><a href="{{url}}{{pages-1}}">{{pages-1}}</a></li>
          <li><a href="{{url}}{{pages}}">{{pages}}</a></li>
        {% elif 0 <= endnum <= 6 and page > 5 %}
          <li><a href="{{url}}1">1</a></li>
          <li><a href="{{url}}2">2</a></li>
          <li><a>...</a></li>
          <li {%if pages-7 == page%}class="active"{%end%}><a href="{{url}}{{pages-7}}">{{pages-7}}</a></li>
          <li {%if pages-6 == page%}class="active"{%end%}><a href="{{url}}{{pages-6}}">{{pages-6}}</a></li>
          <li {%if pages-5 == page%}class="active"{%end%}><a href="{{url}}{{pages-5}}">{{pages-5}}</a></li>
          <li {%if pages-4 == page%}class="active"{%end%}><a href="{{url}}{{pages-4}}">{{pages-4}}</a></li>
          <li {%if pages-3 == page%}class="active"{%end%}><a href="{{url}}{{pages-3}}">{{pages-3}}</a></li>
          <li {%if pages-2 == page%}class="active"{%end%}><a href="{{url}}{{pages-2}}">{{pages-2}}</a></li>
          <li {%if pages-1 == page%}class="active"{%end%}><a href="{{url}}{{pages-1}}">{{pages-1}}</a></li>
          <li {%if pages == page%}class="active"{%end%}><a href="{{url}}{{pages}}">{{pages}}</a></li>
        {%end%}
      {% elif pages <= 12 %}
        {% for i in xrange(1,pages+1) %}
          <li {%if i == page%}class="active"{%end%}><a href="{{url}}{{i}}">{{i}}</a></li>
        {%end%}

      {% end %}

      {% if page >= pages %}
      <li class="active"><a>»</a></li>
      {% else %}
      <li class=""><a href="{{url}}{{nextpage}}">»</a></li>
      {% end %}
    {% end %}
    </ul>
</div>
